﻿using SqlSugar;
using System;
using System.Text;
using System.Collections.Generic;
using System.Data;
using GKQC.DbHelper.ViewModels;
using GKQC.Common;

namespace GKQC.DbHelper.DbModels
{
	//OQC检测单单头
	[SugarTable("t_oqc_head")]
	public class OqcHeadModel
	{
		public OqcHeadModel()
        {
			this.Datas = new List<OqcDataModel>();
			this.ElectricViewModels = new List<ElectricViewModel>();
			this.SizeViewModels = new List<SizeViewModel>();
			this.Standard = new StandardView();
			this.TestDate = DateTime.Now;
			this.CreateDate = DateTime.Now;
			this.Pn = string.Empty;
			this.Customer = string.Empty;
			this.CustomerPn = string.Empty; ;
			this.CusttomerOrder = string.Empty;
			this.MadeType = string.Empty;
			this.Machine = string.Empty;
			this.LotNo = string.Empty;;
			this.SourceCode = string.Empty;
			this.SourceNo = string.Empty;
			this.SourceSNo = 0;
			this.Remark = string.Empty;
			this.Dtls = new List<OqcDtlModel>();
			this.CapFrequency = "120HZ";
			this.ApproveUser = string.Empty; 
			this.ChargeReason = string.Empty;
			this.InstructionNo = string.Empty;
			this.Machine = string.Empty;
			this.MadeType = string.Empty;
			this.Remark = string.Empty;
			this.TestTemp = "25";
			this.TubeDateCode = string.Empty;
			this.UseTemp = string.Empty;
			this.CustomerCode = string.Empty;
			this.Approver = string.Empty;
			this.Auditor = string.Empty;
			this.Designer = string.Empty;
		}

		/// <summary>
		/// 流水号
		/// </summary>		
		[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
		public long Id { get; set; }
		/// <summary>
		/// 来源单别
		/// </summary>		
		public string SourceCode { get; set; }
		/// <summary>
		/// 来源单号
		/// </summary>		
		public string SourceNo { get; set; }
		/// <summary>
		/// 来源单项次
		/// </summary>		
		public int SourceSNo { get; set; }
		/// <summary>
		/// 批号
		/// </summary>		
		public string LotNo { get; set; }
		/// <summary>
		/// 检测日期
		/// </summary>		
		public DateTime TestDate { get; set; }
		/// <summary>
		/// 报表ID
		/// </summary>		
		public int ReportId { get; set; }
		/// <summary>
		/// 系列
		/// </summary>		
		public string Series { get; set; }
		/// <summary>
		/// 规格
		/// </summary>		
		public string Spec { get; set; }
		/// <summary>
		/// 尺寸
		/// </summary>		
		public string Size { get; set; }
		/// <summary>
		/// 脚距
		/// </summary>		
		public string Pitch { get; set; }
		/// <summary>
		/// 加工方式
		/// </summary>		
		public string MadeType { get; set; }
		/// <summary>
		/// 料号
		/// </summary>		
		public string Pn { get; set; }
		/// <summary>
		/// 客户
		/// </summary>		
		public string Customer { get; set; }
		/// <summary>
		/// 代号
		/// </summary>		
		public string CustomerCode { get; set; }
		/// <summary>
		/// 客户订单号
		/// </summary>		
		public string CusttomerOrder { get; set; }
		/// <summary>
		/// 出货数量
		/// </summary>		
		public int Qty { get; set; }
		/// <summary>
		/// 检查数量
		/// </summary>		
		public int SampleQty { get; set; }
		/// <summary>
		/// 套管周期
		/// </summary>		
		public string TubeDateCode { get; set; }
		/// <summary>
		/// 机种
		/// </summary>		
		public string Machine { get; set; }
		/// <summary>
		/// 客户料号
		/// </summary>		
		public string CustomerPn { get; set; }
		/// <summary>
		/// 适用温度范围
		/// </summary>		
		public string UseTemp { get; set; }
		/// <summary>
		/// 试验温度
		/// </summary>		
		public string TestTemp { get; set; }
		/// <summary>
		/// CAP频率
		/// </summary>		
		public string CapFrequency { get; set; }
		/// <summary>
		/// 老化电压
		/// </summary>		
		public double AgeingVoltage { get; set; }
		/// <summary>
		/// 老化时间
		/// </summary>		
		public double AgeingTime { get; set; }
		/// <summary>
		/// 老化温度
		/// </summary>		
		public double AgeingTemp { get; set; }
		/// <summary>
		/// 重充电原因
		/// </summary>		
		public string ChargeReason { get; set; }
		/// <summary>
		/// 电气检测数量
		/// </summary>		
		public int ElectricQty { get; set; }
		/// <summary>
		/// 尺寸检测数量
		/// </summary>		
		public int SizeQty { get; set; }
		/// <summary>
		/// 建立日期
		/// </summary>		
		public DateTime CreateDate { get; set; }
		/// <summary>
		/// 建立人
		/// </summary>		
		public string CreateUser { get; set; }
		/// <summary>
		/// 更新日期
		/// </summary>		
		public DateTime? ModiDate { get; set; }
		/// <summary>
		/// 修改人
		/// </summary>		
		public string ModiUser { get; set; }
		/// <summary>
		/// 审核日期
		/// </summary>		
		public DateTime? ApproveDate { get; set; }
		/// <summary>
		/// 审核人
		/// </summary>		
		public string ApproveUser { get; set; }
		/// <summary>
		/// 指令单号
		/// </summary>		
		public string InstructionNo { get; set; }
		/// <summary>
		/// 备注
		/// </summary>		
		public string Remark { get; set; }
		/// <summary>
		/// 容量上限(%)
		/// </summary>		
		public double CapMax { get; set; }
		/// <summary>
		/// 容量下限(%)
		/// </summary>		
		public double CapMin { get; set; }
		/// <summary>
		/// DF上限(%)
		/// </summary>		
		public double DfMax { get; set; }
		/// <summary>
		/// LC上限(μA)
		/// </summary>		
		public double LcMax { get; set; }
		/// <summary>
		/// Z值上限(mΩ)
		/// </summary>		
		public double ZMax { get; set; }
		/// <summary>
		/// ESR上限(mΩ)
		/// </summary>		
		public double EsrMax { get; set; }
		/// <summary>
		/// 本体直径上限(mm)
		/// </summary>		
		public double DMax { get; set; }
		/// <summary>
		/// 本体高度最大值(mm)
		/// </summary>		
		public double LMax { get; set; }
		/// <summary>
		/// 引脚直径(mm)
		/// </summary>		
		public double cp { get; set; }
		/// <summary>
		/// 肩高(mm)
		/// </summary>		
		public double L1 { get; set; }
		/// <summary>
		/// 引腳超出本體長度(mm)
		/// </summary>		
		public double L2 { get; set; }
		/// <summary>
		/// 脚长
		/// </summary>
		[SugarColumn(IsIgnore = true)]
		public double C { get; set; }

		public DateTime? ManufactureDate { get; set; }

		[SugarColumn(IsIgnore = true)]
		public List<OqcDataModel> Datas { get; set; }

		[SugarColumn(IsIgnore = true)]
		public List<ElectricViewModel> ElectricViewModels { get; set; }
		[SugarColumn(IsIgnore = true)]
		public List<SizeViewModel> SizeViewModels { get; set; }
		[SugarColumn(IsIgnore = true)]
		public StandardView Standard { get; set; }

		public double GetVoltage()
		{
			double voltage = 0;
			if (!string.IsNullOrWhiteSpace(this.Spec))
			{
				string[] ss = Spec.Split('/');
				if (ss.Length == 2)
				{
					double.TryParse(ss[1].Replace("V", string.Empty).Replace("v", string.Empty), out voltage);
				}
			}
			return voltage;
		}

		public double GetCap()
		{
			double cap = 0;
			if (!string.IsNullOrWhiteSpace(this.Spec))
			{
				string[] ss = Spec.Split('/');
				if (ss.Length == 2)
				{
					string s = ss[0].Replace("u", string.Empty).Replace("F", string.Empty).Replace("f", string.Empty)
						.Replace("p", string.Empty).Replace("n", string.Empty)
						.Replace("m", string.Empty).Replace("μ", string.Empty);
					double.TryParse(s, out cap);
				}
			}
			return cap;
		}

		public double GetD()
		{
			double D = 0;
			if (!string.IsNullOrWhiteSpace(this.Size) && this.Size.Contains("*"))
			{
				string[] ss = this.Size.Split('*');
				if (!string.IsNullOrWhiteSpace(ss[0].Trim()))
					double.TryParse(ss[0].Trim(), out D);
			}
			return D;
		}

		/// <summary>
		/// 獲取尺寸中的本體高度L ,尺寸格式應為：D*L
		/// </summary>
		/// <returns></returns>
		public double GetL()
		{
			double L = 0;
			if (!string.IsNullOrWhiteSpace(this.Size) && this.Size.Contains("*"))
			{
				string[] ss = this.Size.Split('*');
				if (!string.IsNullOrWhiteSpace(ss[1].Trim()))
					double.TryParse(ss[01].Trim(), out L);
			}
			return L;
		}

		[SugarColumn(IsIgnore = true)]
		public List<OqcDtlModel> Dtls { get; set; }

		#region AQL标准
		[SugarColumn(IsIgnore = true)]
		public double AqlQty0 { get; set; }
		[SugarColumn(IsIgnore = true)]
		public double AqlQty004 { get; set; }
		[SugarColumn(IsIgnore = true)]
		public double AqlQty01 { get; set; }
		[SugarColumn(IsIgnore = true)]
		public double AqlAcceptQty0 { get; set; }
		[SugarColumn(IsIgnore = true)]
		public double AqlAcceptQty004 { get; set; }
		[SugarColumn(IsIgnore = true)]
		public double AqlAcceptQty01 { get; set; }
		[SugarColumn(IsIgnore = true)]
		public double AqlRejectQty0 { get; set; }
		[SugarColumn(IsIgnore = true)]
		public double AqlRejectQty004 { get; set; }
		[SugarColumn(IsIgnore = true)]
		public double AqlRejectQty01 { get; set; }

		#endregion

		#region OQC 008 等报表签名
		[SugarColumn(IsIgnore = true)]
		public string Approver { get; set; }
		[SugarColumn(IsIgnore = true)]
		public string Auditor { get; set; }
		[SugarColumn(IsIgnore = true)]
		public string Designer { get; set; }
		#endregion

		public void GenerateList(int electricQty, int sizeQty)
		{
			ViewModelTrans<OqcDataModel>.GenerateTestList(electricQty, sizeQty, SourceSNo, ElectricViewModels, SizeViewModels);
		}

		public void GenerateDtls()
        {
			if (Dtls != null)
				Dtls.Clear();
			else
				Dtls = new List<OqcDtlModel>();
			for (int i = 0; i < 8; i++)
			{
				OqcDtlModel dtl = new OqcDtlModel();
				dtl.SNo = i + 1;
				if (i < 4) dtl.d = 0.ToString();
				if (i >= 0 && i < 4) dtl.c = 0.ToString();
				if (i == 1) dtl.n = 20.ToString();
				if (i == 2) dtl.n = 5.ToString();
				if (i == 3) dtl.n = 5.ToString();
				if (i == 5) dtl.n = "ok";

				dtl.Item = dtl.GetItem(i);

				Dtls.Add(dtl);
			}
		}
	}
}

